package com.ting.mybatis.executor;

import com.ting.mybatis.config.MyConfiguration;
import com.ting.mybatis.config.MyMapperStatement;
import com.ting.mybatis.parse.SQLTokenParser;
import com.ting.mybatis.pooled.DataSourcePool;
import com.ting.mybatis.reflection.Reflection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 执行器
 * @Author: ting
 */
public class MyExecutor {

    private MyConfiguration myConfiguration;

  //  private DataSourcePool dataSourcePool;

    public MyExecutor(MyConfiguration myConfiguration) {
        this.myConfiguration = myConfiguration;//把opensession中传的myConfiguration传给当前类的成员变量
//        //初始化数据源
//        dataSourcePool=new DataSourcePool(myConfiguration.getMyEnvironment());
    }

    public Object query(MyMapperStatement myMapperStatement,Object parameter){

            //jdbc
            Connection connection = null;
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
        try {

           // connection = dataSourcePool.getConnection();
            connection = DataSourcePool.open();


            //拿sql语句
            String sql = myMapperStatement.getSql();
            //需要把mapper.xml中#{}改成？才可以设置参数
            preparedStatement = connection.prepareStatement(SQLTokenParser.parse(sql));


            //设置参数
            if (parameter instanceof Integer) {
                preparedStatement.setInt(1, (Integer) parameter);
            } else if (parameter instanceof String) {
                //.....其他类型判断
            }

            //预编译语句执行查询
            resultSet = preparedStatement.executeQuery();

            //处理结果，把查询结果映射成对象，mapper中resultType中指定的类型
            return mappingresultSet(resultSet,myMapperStatement.getResultType());


        }catch (Exception e){
            e.printStackTrace();
        }finally {
           if(resultSet!=null){
               try {
                   resultSet.close();
               } catch (SQLException e) {
                   e.printStackTrace();
               }
           }
            if( preparedStatement!=null){
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;

    }
    
    /**
     * 把resultSet的结果映射到resultType类型对象中
     * 反射实现映射
     * */
    
    public Object mappingresultSet(ResultSet resultSet,String resultType){
        
      
        try {
            //拿到这个返回类型的Class
            Class clazz=Class.forName(resultType);
            if(resultSet.next()){
                Object entity = clazz.newInstance();
                //把数据集放到类对象中
                Reflection.setPropertiesToBeanFromResultSet(entity,resultSet);
                return entity;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
